
* =================================
* Paper title: It's All About Political Incentives: Democracy and the Renewable Feed-In Tariff
* Authors: Patrick Bayer (Glasgow) and Johannes Urpelainen (Columbia) 
* Journal of Politics

* Last modified: November 7, 2015

* Data files used: FIT_data.dta
* System: Stata 13.1 on WIN 10

* Purpose: This do file prepare the data for analysis
* =================================

* =================================
* THIS FILE IS CALLED AUTOMATICALLY FROM 'analysis.do' AND 'appendix.do'
* =================================

*NB: Place all files in the same folder for paths to work properly


clear all
set more off, permanently

* load data
use "FIT_data.dta"


* =================================
* 1 Create and label additional variables
* =================================

xtset id year

drop cowcode politycode continent

gen t = year-1990
gen t2 = t*t
gen t3 = t2*t

label variable t "Linear time trend"
label variable t2 "Quadratic time trend"
label variable t3 "Cubic time trend"

gen log_gdppc = log(gdppc)
label variable log_gdppc "GDP per capita (logged)"

gen co2pc = (co2kt*1000)/ pop
label variable co2pc "CO2 emissions per capita"

gen logco2pc = log(co2pc)
label variable logco2pc "CO2 emissions per capita (logged)"

gen odapc = oda / pop
label variable odapc "ODA per capita"

gen logodapc = log(odapc)
label variable logodapc "ODA per capita (logged)"

replace polity = 0 if polity < -10

replace corruption = corruption * (-1) + 6
replace region = "Asia" if country=="Taiwan"
drop regionid
encode region, gen(regionid)
label variable regionid "Region ID"

replace mdmh=. if mdmh==888 | mdmh==-999
gen logmdmh = log(mdmh)
gen L1logmdmh = L1.logmdmh
label variable L1logmdmh "Mean district magnitude (log, t-1)"

replace mdms=. if mdms==888 | mdms==-999
gen logmdms = log(mdms)
gen L1logmdms = L1.logmdms
label variable L1logmdms "Mean district magnitude (log, t-1)"


gen president = .
replace president = 0 if regime == 0
replace president = 1 if regime == 2 | regime == 3
gen L1president = L1.president
label variable L1president "Presidential system (t-1)"

gen L1pres = L1.pres
label variable L1pres "Presidential system (t-1)"


replace OECD=0 if OECD==.

* Transition variables
gen transition = .
replace transition = 0 if democracy-L5.democracy == 0
replace transition = 1 if democracy-L5.democracy == 1
gen L1transition = L1.transition
label variable L1transition "Transitional democracy (t-1)"
label variable transition "Transitional democracy"

gen politytransition = .
replace politytransition = 0 if polity < 7 | (polity > 6 & polity !=. & L5.polity > 6)
replace politytransition = 1 if polity > 6 & polity !=. & L5.polity < 7
gen L1politytransition = L1.politytransition
label variable L1politytransition "Transitional democracy, Polity IV (t-1)"
label variable politytransition "Transitional democracy, Polity IV"



* =================================
* 1 Create lagged variables for main specification
* =================================

gen L1democracy = L1.democracy
label variable L1democracy "Democracy dummy (t-1)"

gen L1polity = L1.polity
label variable L1polity "Polity IV (t-1)"

label variable malapp "Malapportionment"

gen L1envpref = L1.envpref
label variable L1envpref "Env preference (t-1)"

* Basic set of variables

gen logpop = log(pop)
label variable logpop "Population (log)"

gen L1logpop = L1.logpop
label variable L1logpop "Population (log, t-1)"

gen L1loggdppc = L1.log_gdppc
label variable L1loggdppc "GDP per capita (log, t-1)"

gen L1corruption = L1.corruption
label variable L1corruption "Corruption (t-1)"

gen L1agriemploy = L1.agriemploy
label variable L1agriemploy "Employment in agriculture (t-1)"

gen logland = log(land)
label variable logland "Land area (log)"
gen L1logland = L1.logland
label variable L1logland "Land area (log, t-1)"

* Energy variables

gen L1logco2pc = L1.logco2pc
label variable L1logco2pc "CO2 emissions per capita (log, t-1)"

gen L1fossilfuel = L1.fossilfuel
label variable L1fossilfuel "Fossil fuels in energy consumption (t-1)"

gen logelecgen = log(elecgen)
label variable logelecgen "Total electricity generation (log)"

gen L1elecgen = L1.logelecgen
label variable L1elecgen "Total electricity generation (log, t-1)"

gen L1reform = L1.reform
label variable L1reform "Power sector reform (t-1)"


* International integration

gen L1trade = L1.trade
label variable L1trade "Trade (t-1)"

gen L1logtrade = log(L1trade)
label variable L1logtrade "Trade (log, t-1)"

gen L1logodapc = L1.logodapc
label variable L1logodapc "ODA per capita (log, t-1)"

gen L1fdi = L1.fdi
gen L1logfdi = log(L1fdi)
label variable L1fdi "FDI (t-1)"
label variable L1logfdi "FDI (log, t-1)"

gen L1kyoto = L1.kyoto
label variable L1kyoto "Kyoto ratification (t-1)"

* Additional controls

gen L1polcon3 = L1.polcon3
label variable L1polcon3 "Political constraints (t-1)"

gen L1exselec = L1.exselec
label variable L1exselec "Mode of executive selection (t-1)"

gen L1eiec = L1.eiec
label variable L1eiec "Executive constraints (t-1)"

gen L1leftdummy = L1.leftdummy
label variable L1leftdummy "Left government (t-1)"
label variable leftdummy "Left government"

gen L1federal = L1.federal
label variable L1federal "Federalism (t-1)"

gen L1lrc = L1.lrc
label variable L1lrc "Partisanship (t-1)"

gen loghydrogen = log(hydrogen)
gen L1loghydrogen = L1.loghydrogen
label variable L1loghydrogen "Hydroelectricity generation (log, t-1)"

gen lognonhydrogen = log(nonhydrogen)
gen L1lognonhydrogen = L1.lognonhydrogen
label variable L1lognonhydrogen "Nonhydro renewable electricity generation (log, t-1)"

gen L1oilprice = L1.oil_price
label variable L1oilprice "Oil price per barrel (2010 constant USD, t-1)"

gen logen_import = log(en_import)
gen L1logen_import = L1.logen_import
label variable L1logen_import "Energy imports (log, t-1)"

gen L1elecloss = L1.elecloss
label variable L1elecloss "Transmission and distribution loss (t-1)"

gen elecscarcity = eleccon/elecgen
gen logelec_scarcity = log(elecscarcity)
gen L1logelec_scarcity = L1.logelec_scarcity
label variable L1logelec_scarcity "Electricity scarcity (t-1)"

replace CDMprojects=0 if CDMprojects==.
gen L1cdm = L1.CDMprojects
label variable L1cdm "CDM project count (t-1)"

label variable neighbors "Neighboring countries (count)"

sort country year
by country: gen cumcontiguity = sum(contiguity)
gen cumcontiguity_norm = cumcontiguity/neighbors
xtset id year
gen L1diffusion_norm = L1.cumcontiguity_norm
label variable L1diffusion_norm "FIT diffusion (t-1)" 


sort country year
by country: gen cumRPScontiguity = sum(RPScontiguity)
gen cumRPScontiguity_norm = cumRPScontiguity/neighbors
xtset id year
gen L1RPSdiffusion_norm = L1.cumRPScontiguity_norm
label variable L1RPSdiffusion_norm "RPS diffusion (t-1)" 



* Create dummy variable for low income group
gen lowincome = 0
replace lowincome = 1 if iso3=="AFG" | iso3=="BGD" | iso3=="BEN" | iso3=="BFA" | ///
iso3=="BDI" | iso3=="KHM" | iso3=="CAF" | iso3=="TCD" | iso3=="COM" | iso3=="COD" | ///
iso3=="ERI" | iso3=="ETH" | iso3=="GMB" | iso3=="GIN" | iso3=="GNB" | iso3=="HTI" | ///
iso3=="KEN" | iso3=="PRK" | iso3=="LBR" | iso3=="MDG" | iso3=="MWI" | iso3=="MLI" | ///
iso3=="MOZ" | iso3=="MMR" | iso3=="NPL" | iso3=="NER" | iso3=="RWA" | iso3=="SLE" | ///
iso3=="SOM" | iso3=="TJK" | iso3=="TZA" | iso3=="TGO" | iso3=="UGA" | iso3=="ZWE"

label variable lowincome "Low income country"

* Western European countries according to UN definition
* See: http://unstats.un.org/unsd/methods/m49/m49regin.htm#europe
gen WEU=0
replace WEU=1 if country=="Austria"
replace WEU=1 if country=="Belgium"
replace WEU=1 if country=="France"
replace WEU=1 if country=="Germany"
replace WEU=1 if country=="Liechtenstein"
replace WEU=1 if country=="Luxembourg"
replace WEU=1 if country=="Monaco"
replace WEU=1 if country=="Netherlands"
replace WEU=1 if country=="Switzerland"


* Variable for all developing countries
* See: http://unstats.un.org/unsd/methods/m49/m49regin.htm#developed
gen dev=1
replace dev=0 if country=="Canada"
replace dev=0 if country=="United States"
replace dev=0 if country=="Australia"
replace dev=0 if country=="New Zealand"
replace dev=0 if country=="Japan"
replace dev=0 if region=="Europe"


* =================================
* 2 Count of regional FIT
* =================================

preserve

collapse (sum) FIT (count) id, by(region year)
by region: gen cumFIT=sum(FIT)

gen FITshare = FIT/id
gen cumFITshare = cumFIT/id

encode region, gen(regionid)
xtset regionid year

gen L1FITshare = L1.FITshare
gen L1cumFITshare = L1.cumFITshare

label variable L1FITshare "Share of FITs per region (t-1)"
label variable L1cumFITshare "Share of cum. FITs per region (t-1)"

save "FITdiffusion.dta", replace

restore 

merge m:1 region year using "FITdiffusion.dta"
erase "FITdiffusion.dta"

drop _merge cumFIT FITshare cumFITshare

order country id iso3 iso3n year region regionid


* =================================
* 3 Fill in blank labels
* =================================

label variable WDIaggregate "Aggregate region in WDI data (dummy)"

label variable envpref "Environmental preferences (World Values Survey)"

label variable logmdmh "Mean district magnitude (log, lower chamber)"
label variable logmdms "Mean district magnitude (log, upper chamber)"
label variable president "Presidential system"

label variable loghydrogen "Hydroelectricity generation (log)"
label variable lognonhydrogen "Nonhydro renewable electricity generation (log)"
label variable logen_import "Energy imports (log)"
label variable elecscarcity "Electricity scarcity"
label variable logelec_scarcity "Electricity scarcity (log)"

label variable cumcontiguity "FIT adoption in neighbor countries (\#)"
label variable cumcontiguity_norm "FIT adoption in neighbor countries (normalized \#)"
label variable cumRPScontiguity "RPS adoption in neighbor countries (\#)"
label variable cumRPScontiguity_norm "RPS adoption in neighbor countries (normalized \#)"

label variable WEU "Western European country"
label variable dev "Developing country"
